package com.mashibing.service.impl;

import com.mashibing.internalcommon.constant.CommonStatusEnum;
import com.mashibing.internalcommon.constant.IdentityConstants;
import com.mashibing.internalcommon.constant.TokenConstants;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.request.VerificationCodeDTO;
import com.mashibing.internalcommon.response.NumberCodeResponse;
import com.mashibing.internalcommon.response.TokenResponse;
import com.mashibing.internalcommon.util.JwtUtils;
import com.mashibing.internalcommon.util.RedisPrefixUtils;
import com.mashibing.remote.ServicePassengerUserClient;
import com.mashibing.remote.ServiceVerificationCodeClient;
import com.mashibing.service.VerificationCodeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class VerificationCodeServiceImpl implements VerificationCodeService {

    @Autowired
    private ServiceVerificationCodeClient codeClient;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ServicePassengerUserClient servicePassengerUserClient;

    @Override
    public ResponseResult<Void> generatorCode(VerificationCodeDTO verificationCodeDTO) {
        // 获取验证码
        ResponseResult<NumberCodeResponse> numberCode = codeClient.getNumberCode(6);
        int code = numberCode.getData().getNumberCode();
        System.out.println("获取验证码成功:" + code);

        // 放入redis
        String key = RedisPrefixUtils.generatorKeyByPhone(verificationCodeDTO.getPassengerPhone(), IdentityConstants.PASSENGER_IDENTITY);
        redisTemplate.opsForValue().set(key, code + "", 2, TimeUnit.MINUTES);
        System.out.println("放入redis成功");

        return ResponseResult.success();
    }

    @Override
    public ResponseResult checkVerificationCode(VerificationCodeDTO verificationCodeDTO) {

        String passengerPhone = verificationCodeDTO.getPassengerPhone();

        // 查询redis
        String key = RedisPrefixUtils.generatorKeyByPhone(passengerPhone, IdentityConstants.PASSENGER_IDENTITY);
        String redisCode = redisTemplate.opsForValue().get(key);
        System.out.println("redis中的验证码为：" + redisCode);

        // 验证验证码
        if (StringUtils.isBlank(redisCode) || !redisCode.equals(verificationCodeDTO.getVerificationCode())) {
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(), CommonStatusEnum.VERIFICATION_CODE_ERROR.getMessage());
        }

        // 查看用户是否存在,不存在则新增数据
        servicePassengerUserClient.loginOrRegister(verificationCodeDTO);

        // 生成accessToken
        String accessToken = JwtUtils.generatorToken(passengerPhone, IdentityConstants.PASSENGER_IDENTITY,TokenConstants.ACCESS_TOKEN_TYPE);
        // accessToken放入redis
        String redisAccessTokenKey = RedisPrefixUtils.generatorTokenKey(passengerPhone, IdentityConstants.PASSENGER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
        redisTemplate.opsForValue().set(redisAccessTokenKey,accessToken,30,TimeUnit.DAYS);

        // 生成refreshToken
        String refreshToken = JwtUtils.generatorToken(passengerPhone, IdentityConstants.PASSENGER_IDENTITY,TokenConstants.REFRESH_TOKEN_TYPE);
        // refreshToken放入redis
        String redisRefreshTokenKey = RedisPrefixUtils.generatorTokenKey(passengerPhone, IdentityConstants.PASSENGER_IDENTITY, TokenConstants.REFRESH_TOKEN_TYPE);
        redisTemplate.opsForValue().set(redisRefreshTokenKey,refreshToken,31,TimeUnit.DAYS);

        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken);
        tokenResponse.setRefreshToken(refreshToken);
        return ResponseResult.success(tokenResponse);
    }


}
